//
//  STTools_ChildInterestionTableViewCell.m
//  FindFriend
//
//  Created by apple on 2020/9/3.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THCommentItemView.h"

@interface THCommentItemView()<STBWBrowserViewControllerDelegate>
@property(nonatomic, strong) UIView                     *whiteView;/**<  */
@property(nonatomic, strong) UIView                     *imageContentView;/**<  */

@property(nonatomic, strong) STButton                     *iconButton;/**<  */
@property(nonatomic, strong) STLabel                     *nickLable;/**<  */
@property(nonatomic, strong) STLabel                     *timeLable;/**<  */
@property(nonatomic, strong) STLabel                     *content_Lable;/**<  */

@property(nonatomic, strong) STButton                     *goodButton;/**<  */
@property(nonatomic, strong) CALayer                     *sublayer;/**<  */
@end
@implementation THCommentItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self stoneobs_ui_configSubView];
    }
    return self;
}
- (void)setModel:(THCommentModel *)model{
    _model = model;
    [self stoneobs_ui_configSubView];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    [self removeAllSubviews];
    __weak typeof(self) weakSelf =  self;
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - 0, self.height -0)];
    self.whiteView = whiteView;
    whiteView.layer.cornerRadius = 2;
    whiteView.backgroundColor = UIColor.whiteColor;
    whiteView.clipsToBounds = YES;
    [self addSubview:whiteView];
    self.whiteView = whiteView;
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(10, 15, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:0
                                                        backgroundColor:nil
                                                        backgroundImage:STFDRandomImageProvider.random_livegirl_image
                                                                  image:nil];
        
        [stoneobs_ui_button sd_setImageWithURL:self.model.header_img.mj_url forState:UIControlStateNormal];
        [stoneobs_ui_button stbw_setBorderWith:2 borderColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR cornerRadius:20];
        [self.whiteView addSubview:stoneobs_ui_button];
        self.iconButton = stoneobs_ui_button;
        __weak typeof(self) weakSelf =  self;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [TMUtils goto_user_info_witdh_id:weakSelf.model.user_id];
        }];
        
        
    });

    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, self.iconButton.top, 200, 20)
                                                                text:self.model.user_name
                                                           textColor:UIColor.blackColor
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.whiteView addSubview:stoneobs_ui_label];
        self.nickLable = stoneobs_ui_label;
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
    });
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, self.nickLable.bottom +3, UISCREEN_WIDTH - 120, 20)
                                                                text:[NSString stringWithFormat:@"%@ %@",self.model.created_at,self.model.spec]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.whiteView addSubview:stoneobs_ui_label];
        self.timeLable = stoneobs_ui_label;

    });
    

    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(10, self.iconButton.bottom +10, self.whiteView.width-20, 20)
                                                                text:self.model.evaluate
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [self.whiteView addSubview:stoneobs_ui_label];
        self.content_Lable = stoneobs_ui_label;

    });
    
    
    UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(10, self.content_Lable.bottom + 10, self.whiteView.width - 20, 105)];
    [self.whiteView addSubview:contentView];
    self.imageContentView = contentView;
    whiteView.height = contentView.bottom + 10;
    self.height = whiteView.bottom;

    [self.imageContentView removeAllSubviews];
    CGFloat imageWitdh = (self.imageContentView.width - 2*10 - 5)/3;
    NSInteger min = MIN(self.model.imgs.count, 999999);
    for (NSInteger i = 0; i < min; i ++) {

        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWitdh, imageWitdh)];
        NSString * url = self.model.imgs[i];
        [stoneobs_ui_imageView sd_setImageWithURL:url.mj_url];
        [self.imageContentView addSubview:stoneobs_ui_imageView];
        stoneobs_ui_imageView.layer.cornerRadius = 4;
        stoneobs_ui_imageView.backgroundColor = WH_UICOLOR_FROM_RGBA(0xF6F6F6);
        stoneobs_ui_imageView.clipsToBounds = YES;
        stoneobs_ui_imageView.contentMode = UIViewContentModeScaleAspectFill;
        stoneobs_ui_imageView.userInteractionEnabled = YES;
        stoneobs_ui_imageView.left = imageWitdh * (i%3) + 10*(i%3);
        stoneobs_ui_imageView.top = stoneobs_ui_imageView.height * (i/3) + 10*(i/3);
        UITapGestureRecognizer * tap = [UITapGestureRecognizer.alloc initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf onSelctedButtonWithIndex:i];
        }];
        [stoneobs_ui_imageView addGestureRecognizer:tap];
        contentView.height = stoneobs_ui_imageView.bottom + 10;
    }
    whiteView.height = contentView.bottom + 10;
    self.height = whiteView.bottom;
    self.model.cus_height = self.height;
}
- (void)onSelctedButtonWithIndex:(NSInteger)index{
    //点击了某个图片
    NSMutableArray * models = [NSMutableArray new];
    NSArray * modeArray = self.imageContentView.subviews;
    for (UIImageView * url in modeArray) {
        STBWPhotoModel * model = [STBWPhotoModel  new];
        model.originImage = url.image;
        [models addObject:model];
    }
    STBWBrowserViewController * vc = [[STBWBrowserViewController alloc] initWithArray:models curentIndex:index];
    vc.shouldHideBottomView = YES;
    vc.delegate = self;
    vc.shouldHideTopView = YES;
    UIViewController * rootVC = TMUtils.stoneobs_fun_get_currentTopController;
    [rootVC presentViewController:vc animated:NO completion:nil];
}

#pragma mark --STBWBrowserViewControllerDelegate
- (UIView*)browserControllerDidScrollToIndexpath:(NSIndexPath *)indexPath model:(STBWPhotoModel *)model controller:(STBWBrowserViewController *)controller{

    return   self.imageContentView.subviews[indexPath.row];
}
- (void)rightBarActionFromController:(STBWBrowserViewController *)controller currentIndexPath:(NSIndexPath *)curentIndexpath{


}
- (void)layoutSubviews{
    [self.sublayer removeFromSuperlayer];
//    self.sublayer =  [self.whiteView stoneobs_fun_st_showRoundLayer];
}
@end
